<?php
require_once '../inc/config.php';
require_once '../inc/connexion.php';

$id = isset($_GET['id']) ? $_GET['id'] : "";  
if (isset($_POST['envoyer']) && isset($_FILES['fichier'])){
 $copie_fichier =   move_uploaded_file($_FILES['fichier']['tmp_name'], "../../../images/".$_FILES['fichier']['name']);
 $photo = "/images/".$_FILES['fichier']['name'];
 
     if(isset($_POST['nom']))      $nom=$_POST['nom'];
        else      $nom="";
     if(isset($_POST['prenom']))      $prenom=$_POST['prenom'];
        else      $prenom="";
     if(isset($_POST['liste_entreprise']))      $id_entreprise=$_POST['liste_entreprise'];
        else      $id_entreprise="";
     if(isset($_POST['telephone']))      $telephone=$_POST['telephone'];
        else      $telephone="";
     if(isset($_POST['email']))      $email=$_POST['email'];
        else      $email="";
     if(isset($_POST['observation']))      $observation=$_POST['observation'];
        else      $observation="";
      $sql = "UPDATE contacts SET id_entreprise=$id_entreprise, nom='$nom', prenom='$prenom', telephone=$telephone, email='$email',photo='$photo', observations='$observation' WHERE id = '$id'";
      $envoi_requete = mysqli_query($connexion_serveur, $sql) or die('Erreur SQL !<br />'.$sql.'<br />'.mysql_error());;
   echo '  <h3 style="color:#FF0000;">CONTACT MODIFIE</h3> ';   
   }  
?>
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <meta charset="UTF-8">
        <title>MODIFIER UN CONTACT</title>
        <link href="../bootstrap/css/bootstrap.css" type="text/css" rel="stylesheet">
        <link href="../css/style.css" type="text/css" rel="stylesheet">
        
    </head>
    <body>
    
        <?php  
        
       
        require_once "../inc/css_js_bootstrap/css_js_bootstrap.php"; 
        require_once '../inc/menus/menu_principal.php';
        ?>
        
        <br>
        <div id="div3">
        <input class="btn btn-default" type="button" value="Retour a la liste des contacts" onclick="window.location.href='../contacts/liste_contacts.php'">
        </div>
        <div id="div2" >
        <h2>Modifier un contact</h2><br>
        <?php
        $sql = "SELECT * FROM contacts where id = $id";
        $envoi_requete = mysqli_query($connexion_serveur, $sql) or die('Erreur SQL !<br />'.$sql.'<br />'.mysql_error());;
        while($resultats = mysqli_fetch_array($envoi_requete)) { ?>
          </div>
        <div id="div1" >
        <form name="ajouter_contact" id="ajouter_contact" method="post" action="" enctype="multipart/form-data">
            <table>
                <tr>
                    <td class="col1">Nom:</td>
                    <td class="col2"><input type="text" name="nom" id="nom" maxlength="25" size="25" value="<?php echo $resultats['nom'];?>"></td>
                </tr>
                <tr>
                    <td class="col1">Prenom:</td>
                    <td class="col2"><input type="text" name="prenom" id="prenom" maxlength="25" size="25" value="<?php echo $resultats['prenom'];?>"></td>
                </tr>
                <tr>
                    <td class="col1">Entreprise</td>
                    <td class="col2"><select name="liste_entreprise"> 
                            <?php 
                        //REQUETE SQL
                        $sql_ent = "SELECT * FROM entreprises"; //, contacts WHERE entreprises.id = contacts.id_entreprise";
                        $req_ent = mysqli_query($connexion_serveur, $sql_ent);
                        while ($data_ent = mysqli_fetch_assoc($req_ent)){
                            ?>
                        <option value="<?php echo $data_ent['id']; ?>"><?php echo $data_ent['raison_sociale']; ?> <?php echo $data_ent['id']; ?></option> 
                            <?php }?>
                       </select></td>
                </tr>
                <tr>
                    <td class="col1">Telephone:</td>
                    <td class="col2"><input type="text" name="telephone" id="telephone" maxlength="13" size="13" value="<?php echo $resultats['telephone'];?>"></td>
                </tr>    
                <tr>
                    <td class="col1">Email:</td>
                    <td class="col2"><input type="text" name="email" id="email" maxlength="30" size="40" value="<?php echo $resultats['email'];?>"></td>
                </tr> 
                <tr>
                    <td class="col1">Photo</td>
                    <td class="col2" id="imag"><img src="<?php echo $resultats['photo'];  ?>"><br><p><input type="file" name="fichier"></p></td>
                </tr>
                <tr>
                    <td class="col1">Observation:</td>
                    <td class="col2"><textarea name="observation" id="observation" cols="60" rows="5" placeholder="Observation..."></textarea></td>
                </tr> 
                
                <tr>
                    <td class="col1"></td>
                    <td class="col2"><input type="submit" name="envoyer" value="Modifier"></td>
                </tr>
            </table>  
            
            <div style="margin-left:50% ;"><a href="../contacts/suppr_contacts.php?id=<?php echo $resultats['id'];?>" style="color:#FF0000;" ><h4>Supprimer le contact</h4></a></div>
         <?php
        }
        ?></form>
         </div>
       <br>
       <br>
    </body>
</html>
